/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.weed.helper;

import com.weed.common.Log;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;


import java.util.Set;

//import org.apache.velocity.VelocityContext;
//import org.apache.velocity.app.Velocity;
//import org.apache.velocity.exception.MethodInvocationException;
//import org.apache.velocity.exception.ParseErrorException;
//import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * 基于Velocity的模板引擎，用于解析模板。
 * @author Administrator
 */
public class TemplateHelper {
     Log log = com.weed.helper.LogFactory.getLog(TemplateHelper.class);
     
    static TemplateHelper templateHelper = null;
    
    public static TemplateHelper  getInstance(){
        if(templateHelper == null)
            templateHelper = new TemplateHelper();
        return templateHelper;
    }
     /*
    public TemplateHelper(){
        try {
            init();
        } catch (Exception ex) {
          log.error("初始化TemplateHelper出错!",ex); 
        }
    }
   
    protected  void init() throws Exception {
        Properties properties = new Properties();
        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this);
        // properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
        Velocity.init(properties);
    }

    public StringBuffer parseString(HashMap map, String str) {
        StringWriter writer = new StringWriter();
        VelocityContext context = new VelocityContext();
        try {
            Set set = map.keySet();
            Iterator it = set.iterator();
            while (it.hasNext()) {
                Object key  = it.next();
                context.put(String.valueOf(key), map.get(key));
            }
            Velocity.evaluate(context, writer, str, str);
        } catch (ParseErrorException ex) {
           
        } catch (MethodInvocationException ex) {
            
        } catch (ResourceNotFoundException ex) {
            
        } catch (IOException ex) {
            
        }finally{
//            writer = null;
//            context = null;
        }
        return writer.getBuffer();
    }
     private String readFile(String fileName) {
        StringBuffer htmlBuffer = new StringBuffer();
        try {
            InputStream inputStream =new FileInputStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            while (true){
                String line = reader.readLine();
                if (line != null){
                    htmlBuffer.append(line);
                } else {
                    break;
                }
            }
        } catch (Exception iox){
            iox.printStackTrace();
        }
        return htmlBuffer.toString();
    }
    public static void main(String[] args) {
        HashMap map = new HashMap();
        map.put("func_code", "01010001");
        String str = "select * from es_system.func_code where func_code='$func_code'";
        StringBuffer sb =TemplateHelper.getInstance().parseString(map, str);
        System.out.println("sb = " + sb.toString());
    }
    */
}
